import {ref, Ref} from "vue";
import {useGet, usePost} from "@/hooks";
import {GroupItem, TaskItem, UseTaskReturn} from "@/pages/project/type";

// 格式化团队列表
function formatGroupList(list: any[]): GroupItem[] {
    return list.map(item => {
        return {
            groupId: item.groupId,
            groupName: item.groupName
        }
    })
}

//  获取用户所在的团队
export async function getUserGroup(groupList: Ref<GroupItem[]>,
                                   getFail: (message: string) => void) {
    await useGet({
        url: "/v1/group/user_of_group",
        params: {},
        success: (data) => {
            console.log(data)
            groupList.value = formatGroupList(data.list)
        },
        fail: (message) => {
            getFail(message)
        }
    }, true)
}

//  创建项目
export async function createProject(name: string, desc: string, id: number,
                                    createSuccess: () => void,
                                    createFail: (message: string) => void) {
    await usePost({
        url: "/v1/project",
        data: {
            name: name,
            desc: desc,
            groupId: id
        },
        success: () => {
            createSuccess()
        },
        fail: (message: string) => {
            createFail(message)
        }
    }, true)
}

export function useTask(): UseTaskReturn {
    const taskName = ref("")
    const taskPriority = ref("")
    const taskOwner = ref("")
    const taskStatus = ref("")
    const taskTime = ref<number[]>([])

    // 返回更新后task数组
    function updateTask(task: TaskItem[], index: number, del = 0): TaskItem[] {
        const copyList = [...task]
        if (del) {
            copyList.splice(index, 1)
        } else {
            copyList[index] = {
                taskName: taskName.value,
                taskStatus: taskStatus.value,
                taskOwner: taskOwner.value,
                taskPriority: taskPriority.value,
                taskStart: taskTime.value[0],
                taskEnd: taskTime.value[1]
            }
        }

        return copyList
    }

    // 更新taskItem
    function updateTaskItem(taskList: TaskItem[], index: number) {
        taskName.value = taskList[index].taskName
        taskPriority.value = taskList[index].taskPriority
        taskOwner.value = taskList[index].taskOwner
        taskStatus.value = taskList[index].taskStatus
        taskTime.value = [taskList[index].taskStart, taskList[index].taskEnd]
    }

    // 获取taskItem的值
    function getTaskItemValue(): TaskItem {
        return {
            taskName: taskName.value,
            taskPriority: taskPriority.value,
            taskOwner: taskOwner.value,
            taskStatus: taskStatus.value,
            taskStart: taskTime.value[0],
            taskEnd: taskTime.value[1]
        }
    }

    function isQualified(): boolean {
        return (!taskName.value || !taskPriority.value || !taskStatus.value || !taskTime.value.length);
    }

    return {
        taskName,
        taskPriority,
        taskOwner,
        taskStatus,
        taskTime,
        updateTask,
        updateTaskItem,
        getTaskItemValue,
        isQualified
    }
}

export * from "./config"
export * from "./use-all-projects"